<template>
  <div class="login">
    <img class="wave" src="../../src/assets/images/loginimg/wave.png" />

    <div class="container">
      <div class="img">
        <img src="../../src/assets/images/loginimg/bg.svg" />
        <h3>雅信达互动语言网络学习环境</h3>
      </div>

      <div class="login-content">
        <div class="yaxingda">
          <p class="p">
            <b>j</b><i>ASINDA</i><em> &reg;</em><strong>Software</strong>
          </p>
        </div>
        <el-form
          ref="loginForm"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
        >
          <img src="../../src/assets/images/loginimg/avatar.svg" />
          <h3 class="title">登录</h3>
          <div class="my">
            <el-form-item prop="username">
              <el-input
                v-model="loginForm.username"
                type="text"
                auto-complete="off"
                placeholder="账号"
              >
                <svg-icon
                  slot="prefix"
                  icon-class="user"
                  class="el-input__icon input-icon"
                />
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                v-model="loginForm.password"
                type="password"
                auto-complete="off"
                placeholder="密码"
                @keyup.enter.native="handleLogin"
              >
                <svg-icon
                  slot="prefix"
                  icon-class="password"
                  class="el-input__icon input-icon"
                />
              </el-input>
            </el-form-item>
          </div>
          <!--      <el-form-item prop="code" v-if="captchaOnOff">-->
          <!--        <el-input-->
          <!--          v-model="loginForm.code"-->
          <!--          auto-complete="off"-->
          <!--          placeholder="验证码"-->
          <!--          style="width: 63%"-->
          <!--          @keyup.enter.native="handleLogin"-->
          <!--        >-->
          <!--          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />-->
          <!--        </el-input>-->
          <!--        <div class="login-code">-->
          <!--          <img :src="codeUrl" @click="getCode" class="login-code-img"/>-->
          <!--        </div>-->
          <!--      </el-form-item>-->
          <el-checkbox
            v-model="loginForm.rememberMe"
            text-color="#38d39f"
            fill="#38d39f"
            style="margin: 0px 0px 25px 0px"
            >记住密码</el-checkbox
          >
          <el-form-item style="width: 100%">
            <el-button
              :loading="loading"
              size="medium"
              type="primary"
              style="width: 100%"
              @click.native.prevent="handleLogin"
            >
              <span v-if="!loading">登 录</span>
              <span v-else>登 录 中...</span>
            </el-button>
            <div style="float: right" v-if="register">
              <router-link class="link-type" :to="'/register'"
                >立即注册</router-link
              >
            </div>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <div class="el-login-footer">
      <span>Copyright © 2018-2021 jasinda All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      cookiePassword: "",
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" },
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }],
      },
      loading: false,
      // 验证码开关
      captchaOnOff: true,
      // 注册开关
      register: false,
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    getCode() {
      getCodeImg().then((res) => {
        this.captchaOnOff =
          res.captchaOnOff === undefined ? true : res.captchaOnOff;
        if (this.captchaOnOff) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password:
          password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), {
              expires: 30,
            });
            Cookies.set("rememberMe", this.loginForm.rememberMe, {
              expires: 30,
            });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove("rememberMe");
          }
          this.$store
            .dispatch("Login", this.loginForm)
            .then(() => {
              this.$router.push({ path: this.redirect || "/" }).catch(() => {});
            })
            .catch(() => {
              this.loading = false;
              if (this.captchaOnOff) {
                this.getCode();
              }
            });
        }
      });
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
 /* 原背景图片
 background-image: url("../assets/images/login-background.jpg");
   background-image: url("../assets/images/bgc.jpg");
  background-size: cover;*/
}
.el-checkbox__input.is-checked .el-checkbox__inner {
  background-color: #38d39f;
  color: #38d39f;
  background-color: #38d39f;
}

.title {
  margin: 0px auto 30px auto;
  font-size: 30px;
  text-align: left;
  line-height: 30px;
  font-weight: 700;
  color: rgba(0, 0, 0, 0.85);
}
.login-content .el-checkbox {
  display: flex;
  justify-self: start;
}
.login-content .el-form-item.el-form-item--medium {
  margin-top: 20px;
}
.login-content .el-checkbox__inner {
  top: 2px;
}
.el-checkbox .el-checkbox__input.is-checked {
  color: #38d39f !important;
}
.el-checkbox .el-checkbox__label {
  color: #999 !important;
}
.login-content .yaxingda {
  width: 100%;
  height: 150px;
 
}
.login-content .yaxingda i {
  font-weight: 700;
  color: rgba(212, 121, 17, 0.85) !important;
}
.login-content em {
  position: relative;
  top: -5px;
  display: inline-block;
  transform: scale(0.7);
}
.login-content p {
  display: inline-block;
  width: 200px;
  height: 20px;
  line-height: 20px;
  float: right;
}
.login-content .yaxingda b {
  text-shadow: 2px 1px 3px rgb(23, 23, 177);
  color: blue;
  font-size: 18px;
  font-weight: 700;
}
.login-content .el-button--primary:hover {
  background-color: #38d39f;
  opacity: 0.8;
}
.login-content .el-button--primary {
  display: block;
  width: 100%;
  height: 50px;
  border-radius: 25px;
  outline: none;
  border: none;
  background-image: linear-gradient(to right, #32be8f, #38d39f, #32be8f);
  background-size: 200%;
  font-size: 1.2rem;
  color: #fff;
  font-family: "Poppins", sans-serif;
  text-transform: uppercase;
  margin: 1rem 0;
  cursor: pointer;
  transition: 0.5s;
  background-color: #33c191;
}
.login-form .el-input input {
  font-size: 20px;
  line-height: 40px;
  border: 0;
  font-weight: 500;
  outline: 0;
}
.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 38px;
    input {
      height: 40px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 40px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: left;
  color: #333;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 40px;
}

.wave {
  position: fixed;
  bottom: 0;
  left: 0;
  height: 100%;
  z-index: -1;
}

.container {
  width: 100vw;
  height: 100vh;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  grid-gap: 7rem;
  padding: 0 2rem;
}

.img {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  flex-direction: row-reverse;
  flex-wrap: wrap;
}

.login-content {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  text-align: center;
}

.img img {
  width: 500px;
}

form {
  width: 360px;
}

.login-content img {
  height: 100px;
  display: block;
  margin: 0 auto 40px auto;
}

.login-content h2 {
  margin: 15px 0;
  color: #333;
  text-transform: uppercase;
  font-size: 2.9rem;
}

.login-content .input-div {
  position: relative;
  display: grid;
  grid-template-columns: 7% 93%;
  margin: 25px 0;
  padding: 5px 0;
  border-bottom: 2px solid #d9d9d9;
}

.login-content .input-div.one {
  margin-top: 0;
}

.i {
  color: #d9d9d9;
  display: flex;
  justify-content: center;
  align-items: center;
}

.i i {
  transition: 0.3s;
}

.input-div > div {
  position: relative;
  height: 45px;
}

.input-div > div > h5 {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  color: #999;
  font-size: 18px;
  transition: 0.3s;
}
.login-content .el-form-item__error {
  top: -10px;
  left: 30px;
  color: #606266;
  font-weight: 200;
}

.input-div:before {
  right: 50%;
}

.input-div:after {
  left: 50%;
}

.input-div.focus:before,
.input-div.focus:after {
  width: 50%;
}

.input-div.focus > div > h5 {
  top: -5px;
  font-size: 15px;
}

.input-div.focus > .i > i {
  color: #38d39f;
}

.input-div > div > input {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  border: none;
  outline: none;
  background: none;
  padding: 0.5rem 0.7rem;
  font-size: 1.2rem;
  color: #555;
  font-family: "poppins", sans-serif;
}

.input-div.pass {
  margin-bottom: 4px;
}

a:hover {
  color: #38d39f;
}

.login-form .el-input_inner {
  display: block;
  width: 100%;
  height: 50px;
  border-radius: 25px;
  outline: none;
  border: none;
  background-image: linear-gradient(to right, #32be8f, #38d39f, #32be8f);
  background-size: 200%;
  font-size: 1.2rem;
  color: #fff;
  font-family: "Poppins", sans-serif;
  text-transform: uppercase;
  margin: 1rem 0;
  cursor: pointer;
  transition: 0.5s;
}
.el-input_inner:hover {
  background-position: right;
}

@media screen and (max-width: 1050px) {
  .container {
    grid-gap: 5rem;
  }
}

@media screen and (max-width: 1000px) {
  form {
    width: 290px;
  }

  .login-content h2 {
    font-size: 2.4rem;
    margin: 8px 0;
  }

  .img img {
    width: 400px;
  }
  .title {
    margin: 0px auto 30px auto;
    font-size: 30px;

    line-height: 30px;
    font-weight: 700;
    color: rgba(0, 0, 0, 0.85);
  }
  .login-content .el-checkbox {
    width: 100%;
    margin: 20px 0 20px 0 !important;
  }
}

@media screen and (max-width: 900px) {
  .container {
    grid-template-columns: 1fr;
  }
  .title {
    text-align: center;
  }
  .container .login-content .el-checkbox {
    display: flex;
    justify-self: center;
  }
  .el-login-footer {
    text-align: center;
  }
  .img {
    display: none;
  }
  .login {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    background-image: url("../assets/images/login-background.jpg");
    background-size: cover;
  }
  .wave {
    display: none;
  }
}
.login-content {
  flex-direction: column;
  justify-content: center;
  flex-direction: column;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
}
.my .el-form-item__content::after {
  content: "";
  position: absolute;
  bottom: -2px;
  left: 50%;
  transform: translateX(-50%);
  width: 0%;
  height: 1px;
  transition: 1s;
  z-index: 555555;
  background-color: #38d39f;
}

.my .el-form-item__content:hover:after {
  content: "";
  position: absolute;
  bottom: -2px;
  width: 100%;
  height: 2px;
  z-index: 555555;
  background-color: #38d39f;
}
.el-form-item__content:hover .el-input__icon {
  color: #38d39f;
}
</style>
